package com.starhub.application.rag.util;

import com.starhub.application.rag.constant.RagConstant;

import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.splitter.DocumentByRegexSplitter;
import dev.langchain4j.data.document.splitter.DocumentByWordSplitter;
import dev.langchain4j.data.document.splitter.DocumentSplitters;

/**
 * 文档分割工具类
 * @version: V1.0
 * @author: liuhf
 * @time 2025-06-05
 */
public class DocumentSplitterUtil {

    /**
     * 获取文档分割器
     * @param _maxParagraphSize
     * @param _maxOverlapSize
     * @param splitStrategy
     * @return
     */
    public static DocumentSplitter getDocumentSplitter(Integer _maxParagraphSize, Integer _maxOverlapSize, Integer splitStrategy) {
        int maxParagraphSize = (_maxParagraphSize == null || _maxParagraphSize <= 0) ? RagConstant.DocumentSplitterData.MAX_PARAGRAPH_SIZE : _maxParagraphSize;
        int maxOverlapSize = (_maxOverlapSize == null || _maxOverlapSize <= 0) ? RagConstant.DocumentSplitterData.MAX_OVERLAP_SIZE : _maxOverlapSize;
        
        DocumentSplitter splitter = DocumentSplitters.recursive(maxParagraphSize, maxOverlapSize); // 默认分割
        if (splitStrategy == 0) {
            // 根据字符分割
            DocumentSplitter subSplitter = new DocumentByWordSplitter(maxParagraphSize, maxOverlapSize);
            splitter = new DocumentByRegexSplitter("\n\n", "\n", maxParagraphSize, maxOverlapSize, subSplitter);
        }else{

        }
        
        return splitter;
    }
}
